package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.FilletImageView;
import com.wude.bean.GiftInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.dialog.GiftDialog;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.xutils.x;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 单个礼包Holder
 * Created by Administrator on 2017/3/13.
 */
@SuppressLint("HandlerLeak")
public class DanGiftHolder extends BaseHolder<GiftInfo> {
    @BindView(R.id.gift_icon)
    FilletImageView giftIcon;
    @BindView(R.id.gift_name)
    TextView giftName;
    @BindView(R.id.look)
    TextView look;
    @BindView(R.id.shengyu)
    TextView shengyu;
    @BindView(R.id.gift_context)
    TextView giftContext;
    private Activity activity;
    private GiftInfo giftInfo;

    @Override
    protected void refreshView(GiftInfo giftInfo, int position, Activity activity) {
        this.activity = activity;
        this.giftInfo = giftInfo;
        look.setText("领取");
        Utils.Fill(giftIcon, giftInfo.icon);
        giftName.setText(giftInfo.giftbag_name);
        shengyu.setText("剩余：" + giftInfo.num);
        giftContext.setText(giftInfo.jieshao);
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_gift_dan, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @OnClick(R.id.look)
    public void onClick() {
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser == null) {
            ToastUtil.showToast("请先登录");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        map.put("gift_id", giftInfo.gift_id + "");
        HttpCom.POST(handler, HttpCom.getLingQuUrl(), map, false);
    }

    /**
     * 领取激活码
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String a = HttpUtils.DNSLingQu(msg.obj.toString());
                    if (a != null) {
                        final GiftDialog millionDialog = new GiftDialog(activity, R.style.MillionDialogStyle);
                        millionDialog.setTitle(a);
                        millionDialog.show();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
